package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.excpetion.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.dtos.UserRelationDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.WmUserMapper;
import com.heima.user.service.WmUserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 用户关注/取消关注
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDTO dto) {
        //参数校验
        {
            if (dto == null || dto.getAuthorApUserId() == null || dto.getOperation() == null)
                CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //检查当前登录用户Id,和入参用户Id
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null)
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);

        //当前登录Id
        Integer userId = user.getId();

        //操作目标Id
        Integer authorId = dto.getAuthorApUserId();
        if (Objects.equals(userId, authorId))
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "无法关注自己");

        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        //查询关注信息
        Double score = zSetOperations.score(UserRelationConstants.FOLLOW_LIST + userId, authorId);

        //如果用户执行的是关注操作并且score不为null
        if (dto.getOperation() == 0 && score != null)
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "无法重复关注");
        UpdateArticleMess articleMess = new UpdateArticleMess();
        // 用户执行的取消操作
        if (dto.getOperation() == 1) {
            //移除关注关系
            zSetOperations.remove(UserRelationConstants.FOLLOW_LIST + userId, authorId);

            //移除粉丝关系
            zSetOperations.remove(UserRelationConstants.FANS_LIST + authorId, userId);
            articleMess.setAdd(-3);

        }
        // 用户执行的关注操作
        else {
            zSetOperations.add(UserRelationConstants.FOLLOW_LIST + userId, authorId, System.currentTimeMillis());
            zSetOperations.add(UserRelationConstants.FOLLOW_LIST + userId, authorId, System.currentTimeMillis());
            articleMess.setAdd(3);
        }
        //向MQ发送消息
        {
            articleMess.setType(UpdateArticleMess.UpdateArticleType.FOLLOW);
            articleMess.setArticleId(dto.getArticleId());
            //发送消息
            rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(articleMess));
        }
        return ResponseResult.okResult();
    }
}